package com.adminx.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;


public class MyUserDetails extends User{

	private static final long serialVersionUID = 7835566454579368520L;
	
	private Map<String, Object> userInfo;
	
	private Account account;
	
	public MyUserDetails(String username, String password, boolean enabled, boolean accountNonExpired,
			boolean credentialsNonExpired, boolean accountNonLocked,
			Collection<? extends GrantedAuthority> authorities) {
		super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
		userInfo = new HashMap<String, Object>();
	}
	
	public MyUserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) {
		super(username, password, true, true, true, true, authorities);
		userInfo = new HashMap<String, Object>();
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Map<String, Object> getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(Map<String, Object> userInfo) {
		this.userInfo = userInfo;
	}
		
	public void pushUserInfo(String key, Object value) {
		userInfo.put(key, value);
	}
	
	public void removeUserInfo(String key) {
		userInfo.remove(key);
	}

	public void emptyUserInfo() {
		userInfo.clear();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MyUserDetails) {
			MyUserDetails another = (MyUserDetails)obj;
			return this.getUsername().equals(another.getUsername());
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return this.getUsername().hashCode();
	}
}
